[CALCITE-7440] Preserve correlated scope mapping in RelToSqlConverter#4828
Draft
bvolpato wants to merge 2 commits intoapache:mainfrom
Draft
[CALCITE-7440] Preserve correlated scope mapping in RelToSqlConverter#4828bvolpato wants to merge 2 commits intoapache:mainfrom
bvolpato wants to merge 2 commits intoapache:mainfrom
Conversation
6309f8d to
5447f29
Compare
4449f5a to
f11eed6
Compare
xiedeyantu
reviewed
Mar 12, 2026
| * <a href="https://issues.apache.org/jira/browse/CALCITE-7440">[CALCITE-7440] | ||
| * RelToSqlConverter throws NPE when correlation scope is missing after | ||
| * semi-join rewrites.</a>. */ | ||
| @Test void testPostgresqlRoundTripCorrelatedProjectWithSemiJoinRules() { |
Member
There was a problem hiding this comment.
I ran this test on the main branch, and strangely it passed. Could you check if your case can reproduce the problem you mentioned?
@Test void testPostgresqlRoundTripCorrelatedProjectWithSemiJoinRules() {
final String query = "WITH product_keys AS (\n"
+ " SELECT p.\"product_id\",\n"
+ " (SELECT MAX(p3.\"product_id\")\n"
+ " FROM \"foodmart\".\"product\" p3\n"
+ " WHERE p3.\"product_id\" = p.\"product_id\") AS \"mx\"\n"
+ " FROM \"foodmart\".\"product\" p\n"
+ ")\n"
+ "SELECT DISTINCT pk.\"product_id\"\n"
+ "FROM product_keys pk\n"
+ "LEFT JOIN \"foodmart\".\"product\" p2 USING (\"product_id\")\n"
+ "WHERE pk.\"product_id\" IN (\n"
+ " SELECT p4.\"product_id\"\n"
+ " FROM \"foodmart\".\"product\" p4\n"
+ ")";
final RuleSet rules = RuleSets.ofList();
final String generated = sql(query).withPostgresql().optimize(rules, null).exec();
sql(generated).withPostgresql().exec();
}
Member
There was a problem hiding this comment.
Please take a look at my description; you may not have solved my problem.
Dwrite
reviewed
Mar 12, 2026
core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
Show resolved
Hide resolved
Author
|
Thanks for the review! Updated this PR with both requested changes: Reduced the regression to the minimal reproducer on main (empty rules was a mistake):
Added a plain round-trip test without extra rules:
Both new/updated tests pass locally in this branch. |
6365d37 to
565cb6a
Compare
565cb6a to
a685206
Compare
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
RelToSqlConverter failed during PostgreSQL round-trip validation when semi-join rewrites introduced a correlated filter with an unresolved correlation id.
Reproduction query
Error before this PR
Fix
Register missing RexCorrelVariable ids while visiting filter conditions, without overriding existing correlation mappings.
Test
RelToSqlConverterTest.testPostgresqlRoundTripCorrelatedProjectWithSemiJoinRules./gradlew :core:test --tests org.apache.calcite.rel.rel2sql.RelToSqlConverterTestDisclaimer
The changes were done with the assistance of Codex (gpt-5.3-codex) in response to an actual bug when using Calcite in production. The changes were manually verified prior to sending this to review.